<script setup>
import { ref } from 'vue'
import { userRegisterService } from '@/api/user'
const formObj = ref({
  userName: '',
  password: '',
  repassword: '',
  email: '',
  code: ''
})

const isLogin = ref(true)
const isRegister = ref(false)
const rePsd = ref(false)
// 点击立即注册
const goRegister = async () => {
  const res = await userRegisterService(formObj.value)
  console.log(res)
  isLogin.value = false
  isRegister.value = true
}

// 点击忘记密码
const goResetPsd = () => {
  isLogin.value = false
  rePsd.value = true
}

// 点击去登录
const goLogin = () => {
  isLogin.value = true
  isRegister.value = false
  rePsd.value = false
}

// 发送验证码
const sendCode = () => {
  // TODO
}

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    {
      pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,
      message: '邮箱格式不正确，请重新输入',
      trigger: 'blur'
    }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    {
      pattern: /^\d{4}$/,
      message: '验证码格式不正确，请重新输入',
      trigger: 'blur'
    }
  ],
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 10, message: '用户名必须是 4-10位 的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formObj.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback() // 就算校验成功，也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-------------------------------- 登录相关 ---------------------------------------------->
      <el-form
        :model="formObj"
        :rules="rules"
        autocomplete="off"
        v-if="isLogin"
        size="large"
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="userName">
          <el-input
            v-model="formObj.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formObj.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="button">登录</el-button>
        <el-form-item class="end">
          <el-link type="info" :underline="false" @click="goRegister"
            >立即注册</el-link
          >
          <el-link
            type="info"
            class="lk-auto"
            :underline="false"
            @click="goResetPsd"
            >忘记密码</el-link
          >
        </el-form-item>
      </el-form>
      <!-------------------------------- 注册相关 ---------------------------------------------->
      <el-form
        :model="formObj"
        :rules="rules"
        autocomplete="off"
        v-else-if="isRegister"
        size="large"
      >
        <el-form-item class="dl_header"
          ><h1>注册</h1>
          <span class="dl_header_right"
            >已有账号？
            <el-link :underline="false" @click="goLogin">去登录</el-link>
          </span>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="formObj.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="userName">
          <el-input
            v-model="formObj.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formObj.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formObj.repassword"
            placeholder="请再次输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="button" @click="onRegister"
          >注册</el-button
        >
      </el-form>
      <!-------------------------------- 忘记密码 ---------------------------------------------->
      <el-form
        :model="formObj"
        :rules="rules"
        autocomplete="off"
        size="large"
        v-else
      >
        <el-form-item class="dl_header">
          <h1>重置密码</h1>
          <span class="dl_header_right"
            >想起密码？
            <el-link :underline="false" @click="goLogin">去登录</el-link>
          </span>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="formObj.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="formObj.code"
            placeholder="请输入验证码"
            style="width: 250px; margin-right: 12px"
          ></el-input>
          <el-button class="send" style="width: 86px" @click="sendCode"
            >发送</el-button
          >
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formObj.password"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="button">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background-color: pink;
    border-radius: 0 10px 10px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    .button {
      width: 100%;
    }
    .end {
      display: flex;
      justify-content: space-between;
      .lk-auto {
        margin-left: auto;
      }
    }
    .dl_header {
      display: flex;
      justify-content: space-between;
      .dl_header_right {
        margin-left: auto;
      }
    }
  }
}
</style>
